using System;
using UnityEngine;
using UnityEngine.Rendering;
using System.Text;
using System.IO;

namespace LightingRenderingAssitant
{
	public static class LightingRenderingUtilities
	{
        public static StreamWriter smWriter;
        public static void GatherSceneInfo()
        {
            var logPath = Path.Combine(Application.dataPath, "../Log/");
            if (!Directory.Exists(logPath)) Directory.CreateDirectory(logPath);
            var logFile = "test.log";
            var txtFile = Path.Combine(logPath, logFile);
            var of = File.AppendText(txtFile);
            smWriter = of;
            /*
            Application.logMessageReceived += ( condition,  stackTrace,  type) =>
            {
                var exp = "Exception:" + condition + ":" + stackTrace + ":" + type;
                of.WriteLine(exp);
                Console.WriteLine(exp);
            };
            */

            Console.WriteLine("##SceneInfo##");
            var sc = UnityEngine.SceneManagement.SceneManager.GetActiveScene();
            var roots = sc.GetRootGameObjects();

            var sb = new StringBuilder();
            sb.AppendLine("Roots:"+roots.Length);
            GameObject girl = null;
            foreach(var r in roots)
            {
                sb.AppendLine(r.name);
                if (r.name.Contains("chaF"))
                {
                    girl = r;
                }
            }
            Console.WriteLine(sb.ToString());

            Console.WriteLine("##GameObjectInfo");
            try
            {
                MyLib.EntityConfigExport.ExportMethod(girl);
            }catch(Exception exp)
            {
                Console.WriteLine(exp.ToString());
            }
        }


		public static void GatherLightInfo()
		{
			Console.WriteLine("Ambient Mode: " + RenderSettings.ambientMode.ToString());
			float num = RenderSettings.ambientIntensity;
			Console.WriteLine("Ambient Intensity: " + num.ToString());
			Color color = RenderSettings.ambientSkyColor;
			Console.WriteLine("Ambient Sky Color: " + color.ToString());
			color = RenderSettings.ambientEquatorColor;
			Console.WriteLine("Ambient Equator Color: " + color.ToString());
			color = RenderSettings.ambientGroundColor;
			Console.WriteLine("Ambient Ground Color: " + color.ToString());
			num = RenderSettings.reflectionIntensity;
			Console.WriteLine("Renflection Intensity: " + num.ToString());
			Light[] array = UnityEngine.Object.FindObjectsOfType<Light>();
			foreach (Light light in array)
			{
                var sb = new StringBuilder();
                sb.AppendLine(light.name);
                sb.AppendLine("Intensity:" + light.intensity);
                sb.AppendLine("color:"+light.color);
                sb.AppendLine("pos:" + light.transform.position);
                sb.AppendLine("pos:" + light.transform.eulerAngles);
                sb.AppendLine("scale:" + light.transform.localScale);
                sb.AppendLine("Mode:"+light.renderMode);
                sb.AppendLine("shadow:"+light.shadows);
                sb.AppendLine("cookie:"+light.cookie);
                sb.AppendLine("LightType:"+light.type);
                sb.AppendLine("bounce:"+light.bounceIntensity);
                sb.AppendLine("flare:"+light.flare);
                sb.AppendLine("cull:"+light.cullingMask);

                /*
				string[] obj = new string[6]
				{
					light.name.ToString(),
					" Intensity: ",
					null,
					null,
					null,
                    null,
				};
				num = light.intensity;
				obj[2] = num.ToString();
				obj[3] = " Color ";
				color = light.color;
				obj[4] = color.ToString();
                obj[5] = light.transform.position.ToString();
                */
                //Console.WriteLine(string.Concat(obj));
                Console.WriteLine(sb.ToString());
			}
		}

		public static void SetLighting(LightingSetting Lightsettings)
		{
			if (Lightsettings.AmbientMode < 0)
			{
				Lightsettings.AmbientMode = (int)RenderSettings.ambientMode;
			}
			else
			{
				RenderSettings.ambientMode = (AmbientMode)Lightsettings.AmbientMode;
			}
			if (Lightsettings.AmbientIntensity < 0f)
			{
				Lightsettings.AmbientIntensity = RenderSettings.ambientIntensity;
			}
			else
			{
				RenderSettings.ambientIntensity = Lightsettings.AmbientIntensity;
			}
			if (Lightsettings.AmbientMode == 0)
			{
				DynamicGI.UpdateEnvironment();
			}
			if (Lightsettings.AmbientSkyColor == Color.clear)
			{
				Lightsettings.AmbientSkyColor = RenderSettings.ambientSkyColor;
			}
			else
			{
				RenderSettings.ambientSkyColor = Lightsettings.AmbientSkyColor;
			}
			if (Lightsettings.AmbientEquatorColor == Color.clear)
			{
				Lightsettings.AmbientEquatorColor = RenderSettings.ambientEquatorColor;
			}
			else
			{
				RenderSettings.ambientEquatorColor = Lightsettings.AmbientEquatorColor;
			}
			if (Lightsettings.AmbientGoundColor == Color.clear)
			{
				Lightsettings.AmbientGoundColor = RenderSettings.ambientGroundColor;
			}
			else
			{
				RenderSettings.ambientGroundColor = Lightsettings.AmbientGoundColor;
			}
			if (Lightsettings.ReflectionIntensity < 0f)
			{
				Lightsettings.ReflectionIntensity = RenderSettings.reflectionIntensity;
			}
			else
			{
				RenderSettings.reflectionIntensity = Lightsettings.ReflectionIntensity;
			}
			Light[] array = UnityEngine.Object.FindObjectsOfType<Light>();
			foreach (Light light in array)
			{
				if (light.name == "DirectionalFront" || light.name == "Directional Front")
				{
					if (Lightsettings.FrontLightIntensity < 0f)
					{
						Lightsettings.FrontLightIntensity = light.intensity;
					}
					else
					{
						light.intensity = Lightsettings.FrontLightIntensity;
					}
					if (Lightsettings.FrontLightColor == Color.clear)
					{
						Lightsettings.FrontLightColor = light.color;
					}
					else
					{
						light.color = Lightsettings.FrontLightColor;
					}
				}
				else if (light.name == "DirectionalBack" || light.name == "Directional Back")
				{
					if (Lightsettings.BackLightIntensity < 0f)
					{
						Lightsettings.BackLightIntensity = light.intensity;
					}
					else
					{
						light.intensity = Lightsettings.BackLightIntensity;
					}
					if (Lightsettings.BackLightColor == Color.clear)
					{
						Lightsettings.BackLightColor = light.color;
					}
					else
					{
						light.color = Lightsettings.BackLightColor;
					}
				}
				else if (light.name == "DirectionalFrontMap" && Lightsettings.FrontLightColor != Color.clear && Lightsettings.FrontLightIntensity >= 0f)
				{
					light.intensity = Lightsettings.FrontLightIntensity;
					light.color = Lightsettings.FrontLightColor;
				}
			}
		}

		public static void SetLightingKey(LightingSettingKey Lightsettings)
		{
			if (Lightsettings.AmbientMode < 0)
			{
				Lightsettings.AmbientMode = (int)RenderSettings.ambientMode;
			}
			else
			{
				RenderSettings.ambientMode = (AmbientMode)Lightsettings.AmbientMode;
			}
			if (Lightsettings.AmbientIntensity < 0f)
			{
				Lightsettings.AmbientIntensity = RenderSettings.ambientIntensity;
			}
			else
			{
				RenderSettings.ambientIntensity = Lightsettings.AmbientIntensity;
			}
			if (Lightsettings.AmbientMode == 0)
			{
				DynamicGI.UpdateEnvironment();
			}
			if (Lightsettings.AmbientSkyColor == Color.clear)
			{
				Lightsettings.AmbientSkyColor = RenderSettings.ambientSkyColor;
			}
			else
			{
				RenderSettings.ambientSkyColor = Lightsettings.AmbientSkyColor;
			}
			if (Lightsettings.AmbientEquatorColor == Color.clear)
			{
				Lightsettings.AmbientEquatorColor = RenderSettings.ambientEquatorColor;
			}
			else
			{
				RenderSettings.ambientEquatorColor = Lightsettings.AmbientEquatorColor;
			}
			if (Lightsettings.AmbientGoundColor == Color.clear)
			{
				Lightsettings.AmbientGoundColor = RenderSettings.ambientGroundColor;
			}
			else
			{
				RenderSettings.ambientGroundColor = Lightsettings.AmbientGoundColor;
			}
			if (Lightsettings.ReflectionIntensity < 0f)
			{
				Lightsettings.ReflectionIntensity = RenderSettings.reflectionIntensity;
			}
			else
			{
				RenderSettings.reflectionIntensity = Lightsettings.ReflectionIntensity;
			}
			Light[] array = UnityEngine.Object.FindObjectsOfType<Light>();
			foreach (Light light in array)
			{
				if (light.name == "DirectionalFront" || light.name == "Directional Front")
				{
					if (Lightsettings.FrontLightIntensity < 0f)
					{
						Lightsettings.FrontLightIntensity = light.intensity;
					}
					else
					{
						light.intensity = Lightsettings.FrontLightIntensity;
					}
					if (Lightsettings.FrontLightColor == Color.clear)
					{
						Lightsettings.FrontLightColor = light.color;
					}
					else
					{
						light.color = Lightsettings.FrontLightColor;
					}
				}
				else if (light.name == "DirectionalBack" || light.name == "Directional Back")
				{
					if (Lightsettings.BackLightIntensity < 0f)
					{
						Lightsettings.BackLightIntensity = light.intensity;
					}
					else
					{
						light.intensity = Lightsettings.BackLightIntensity;
					}
					if (Lightsettings.BackLightColor == Color.clear)
					{
						Lightsettings.BackLightColor = light.color;
					}
					else
					{
						light.color = Lightsettings.BackLightColor;
					}
				}
				else if (light.name == "DirectionalFrontMap" && Lightsettings.FrontLightColor != Color.clear && Lightsettings.FrontLightIntensity >= 0f)
				{
					light.intensity = Lightsettings.FrontLightIntensity;
					light.color = Lightsettings.FrontLightColor;
				}
				else if (light.name == "Directional Key")
				{
					if (Lightsettings.KeyLightIntensity < 0f)
					{
						Lightsettings.KeyLightIntensity = light.intensity;
					}
					else
					{
						light.intensity = Lightsettings.KeyLightIntensity;
					}
					if (Lightsettings.KeyLightColor == Color.clear)
					{
						Lightsettings.KeyLightColor = light.color;
					}
					else
					{
						light.color = Lightsettings.KeyLightColor;
					}
				}
			}
		}

		public static void SetLightingLobby(LobbyLight Lightsettings)
		{
			if (Lightsettings.AmbientMode < 0)
			{
				Lightsettings.AmbientMode = (int)RenderSettings.ambientMode;
			}
			else
			{
				RenderSettings.ambientMode = (AmbientMode)Lightsettings.AmbientMode;
			}
			if (Lightsettings.AmbientIntensity < 0f)
			{
				Lightsettings.AmbientIntensity = RenderSettings.ambientIntensity;
			}
			else
			{
				RenderSettings.ambientIntensity = Lightsettings.AmbientIntensity;
			}
			if (Lightsettings.AmbientMode == 0)
			{
				DynamicGI.UpdateEnvironment();
			}
			if (Lightsettings.AmbientSkyColor == Color.clear)
			{
				Lightsettings.AmbientSkyColor = RenderSettings.ambientSkyColor;
			}
			else
			{
				RenderSettings.ambientSkyColor = Lightsettings.AmbientSkyColor;
			}
			if (Lightsettings.AmbientEquatorColor == Color.clear)
			{
				Lightsettings.AmbientEquatorColor = RenderSettings.ambientEquatorColor;
			}
			else
			{
				RenderSettings.ambientEquatorColor = Lightsettings.AmbientEquatorColor;
			}
			if (Lightsettings.AmbientGoundColor == Color.clear)
			{
				Lightsettings.AmbientGoundColor = RenderSettings.ambientGroundColor;
			}
			else
			{
				RenderSettings.ambientGroundColor = Lightsettings.AmbientGoundColor;
			}
			if (Lightsettings.ReflectionIntensity < 0f)
			{
				Lightsettings.ReflectionIntensity = RenderSettings.reflectionIntensity;
			}
			else
			{
				RenderSettings.reflectionIntensity = Lightsettings.ReflectionIntensity;
			}
			Light[] array = UnityEngine.Object.FindObjectsOfType<Light>();
			foreach (Light light in array)
			{
				if (light.name == "DirectionalFront" || light.name == "Directional Front")
				{
					if (Lightsettings.FrontLightIntensity < 0f)
					{
						Lightsettings.FrontLightIntensity = light.intensity;
					}
					else
					{
						light.intensity = Lightsettings.FrontLightIntensity;
					}
					if (Lightsettings.FrontLightColor == Color.clear)
					{
						Lightsettings.FrontLightColor = light.color;
					}
					else
					{
						light.color = Lightsettings.FrontLightColor;
					}
				}
				else if (light.name == "DirectionalBack" || light.name == "Directional Back")
				{
					if (Lightsettings.BackLightIntensity < 0f)
					{
						Lightsettings.BackLightIntensity = light.intensity;
					}
					else
					{
						light.intensity = Lightsettings.BackLightIntensity;
					}
					if (Lightsettings.BackLightColor == Color.clear)
					{
						Lightsettings.BackLightColor = light.color;
					}
					else
					{
						light.color = Lightsettings.BackLightColor;
					}
				}
				else if (light.name == "DirectionalFrontMap" && Lightsettings.FrontLightColor != Color.clear && Lightsettings.FrontLightIntensity >= 0f)
				{
					light.intensity = Lightsettings.FrontLightIntensity;
					light.color = Lightsettings.FrontLightColor;
				}
				else if (light.name == "Directional Key")
				{
					if (Lightsettings.KeyLightIntensity < 0f)
					{
						Lightsettings.KeyLightIntensity = light.intensity;
					}
					else
					{
						light.intensity = Lightsettings.KeyLightIntensity;
					}
					if (Lightsettings.KeyLightColor == Color.clear)
					{
						Lightsettings.KeyLightColor = light.color;
					}
					else
					{
						light.color = Lightsettings.KeyLightColor;
					}
				}
				else if (light.name == "map_hs_00_ADVLight(Clone)")
				{
					if (Lightsettings.ADVLightIntensity < 0f)
					{
						Lightsettings.ADVLightIntensity = light.intensity;
					}
					else
					{
						light.intensity = Lightsettings.ADVLightIntensity;
					}
					if (Lightsettings.ADVLightColor == Color.clear)
					{
						Lightsettings.ADVLightColor = light.color;
					}
					else
					{
						light.color = Lightsettings.ADVLightColor;
					}
				}
				else if (light.name == "map_hs_00_SelectLight")
				{
					if (Lightsettings.SelectLightIntensity < 0f)
					{
						Lightsettings.SelectLightIntensity = light.intensity;
					}
					else
					{
						light.intensity = Lightsettings.SelectLightIntensity;
					}
					if (Lightsettings.SelectLightColor == Color.clear)
					{
						Lightsettings.SelectLightColor = light.color;
					}
					else
					{
						light.color = Lightsettings.SelectLightColor;
					}
				}
			}
		}
	}
}
